package com.example.dllo.baidumusic.fragment;
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.dllo.baidumusic.R;
import com.example.dllo.baidumusic.adapter.SingListAdapter;
import com.example.dllo.baidumusic.bean.MusicBean;
import com.example.dllo.baidumusic.bean.PlayBean;
import com.example.dllo.baidumusic.bean.SingListIdBean;
import com.example.dllo.baidumusic.nettool.URLValuas;
import com.example.dllo.baidumusic.events.MusicListEvent;
import com.example.dllo.baidumusic.nettool.BaseFragment;
import com.example.dllo.baidumusic.nettool.NetTool;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/8/20.
 */
public class SingDanListFragment extends BaseFragment implements View.OnClickListener {
    private TextView textView;
    private ImageView img;
    private ListView listView;
    private String SINGLIST_URL;
    private SingListAdapter listAdapter;


    @Override
    protected int setLayout() {
        return R.layout.fragment_hotsing;
    }

    @Override
    protected void initView() {

        listView = bindView(R.id.sing_listview);
        textView = bindView(R.id.sing_text_title);
        listView.addHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.sing_toushitu, null));
        img = bindView(R.id.sing_img);
        ImageButton imageButton = bindView(R.id.sing_btn_fanhui);
        imageButton.setOnClickListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<MusicBean> musicBeanList = getMusicBeanFromArray();
                EventBus.getDefault().post(new MusicListEvent(position - 1, musicBeanList));
            }
        });


    }

    private List<MusicBean> getMusicBeanFromArray() {
        List<MusicBean> musicBeanList = new ArrayList<>();
        for (MusicBean musicBean : musicBeen) {
            musicBeanList.add(musicBean);
        }
        return musicBeanList;
    }

    private MusicBean[] musicBeen;//集合 歌曲信息

    @Override
    protected void initData() {

        listAdapter = new SingListAdapter(getContext());

        SINGLIST_URL = getArguments().getString("XXX");
        final NetTool netTool = new NetTool();

        netTool.getNetData(SINGLIST_URL, SingListIdBean.class, new NetTool.NetListener<SingListIdBean>() {
            @Override
            public void onSuccess(final SingListIdBean singListIdBean) {
                netTool.getImage(singListIdBean.getPic_w700(), img);
                textView.setText(singListIdBean.getTitle());
                if (singListIdBean != null) {

                    listAdapter.setListIdBean(singListIdBean);
                } else {

                    listAdapter.setListIdBean(new SingListIdBean());
                }
                listView.setAdapter(listAdapter);

                if (singListIdBean.getContent() != null) {
                    musicBeen = new MusicBean[singListIdBean.getContent().size()];
                    for (int i = 0; i < singListIdBean.getContent().size(); i++) {
                        getMusicInfo(i, URLValuas.PLAY_FRONT + singListIdBean
                                .getContent().get(i).getSong_id() + URLValuas.PLAY_BEHIND);

                    }


                }
            }

            @Override
            public void onError(String errorMsg) {

            }
        });

    }

    //取数据
    private void getMusicInfo(final int pos, String url) {
        NetTool netTool = new NetTool();
        netTool.getNetData(url, PlayBean.class, new NetTool.NetListener<PlayBean>() {
            @Override
            public void onSuccess(PlayBean playBean) {

                MusicBean musicBean = new MusicBean();
                try {
                    musicBean.setUrl(playBean.getBitrate().getFile_link());
                    musicBean.setMusicName(playBean.getSonginfo().getTitle());
                    musicBean.setSingerName(playBean.getSonginfo().getAuthor());
                    musicBean.setSingerPic(playBean.getSonginfo().getPic_small());
                    musicBean.setBigpic(playBean.getSonginfo().getPic_huge());
                    musicBean.setSmallPic(playBean.getSonginfo().getPic_premium());
                    musicBean.setTitle(playBean.getSonginfo().getAlbum_title());
                    musicBean.setLrcUrl(playBean.getSonginfo().getLrclink());
                    musicBeen[pos] = musicBean;
                } catch (Exception e) {

                    musicBean.setBigpic(playBean.getSonginfo().getPic_big());
                }

            }

            @Override
            public void onError(String errorMsg) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }
}
